'use strict';
angular.module('invoice.recSKHXList', [])
	.controller('recSKHXListCtrl',['$rootScope', '$scope', 'BizData', 'systemService', f])
    function f($rootScope, $scope, BizData, systemService) {

		var Model = this;
		
		Model.dataMasterAfterRefresh = function(event) {
			//console.log('ddddddddddddddd');
			var dataMaster = event.source;
			for(var i=0;i<dataMaster.getCount();i++){
				var rowID = dataMaster.getID(i);
				var billType = dataMaster.getValue('billType',rowID);
				if(billType=='SKDJ'){
					dataMaster.setValue('discountCash',0,rowID);
				}
			}
		};
		
		var options = {};
		options.container = Model;
		options.concept = 'AccountPaymentDetail';
		options.name = 'dataMaster';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '';
		options.reader = '/invoice/accountPaymentDetail/query';
		options.writer = '';
		options.deleter = '';
		options.idColumnDefine = "fID";
		options.idColumnName = "fID";
		options.isTree = false;
		options.parentRelation = 'fParent';
		options.nodeKindRelation = 'nodeKind';
		options.nodeLevelRelation = 'nodeLevel';
		options.pathRelation = '';
		options.master = {"data":"","relation":""};
		options.directDeleteMode = true;
		options.limit = 15;
		options.orderBy = "accountPaymentNo";
		
		options.onAfterRefresh = Model.dataMasterAfterRefresh;

		Model.dataMaster = new BizData(options);

		//////////////////////////////////////////////
		
		
		
		////////////////////启动执行开始///////////////////////////////////////////////////

		//设置过滤条件
		Model.dataMaster.filter = "AccountPaymentDetail.audit='confirm'"	
//			+ " AND AccountPaymentDetail.status='open'"
			+ " AND (AccountPaymentDetail.clearType='6' OR AccountPaymentDetail.clearType='D')"
			+ " AND (AccountPaymentDetail.revOpay='I' OR AccountPaymentDetail.revOpay='9')"
			+ " AND AccountPaymentDetail.billType<>'5'"
			;

		//刷新数据
	    Model.dataMaster.refreshData();

		////////////////////启动执行结束///////////////////////////////////////////////////
	    
		Model.refresh = function(){
			Model.dataMaster.refreshData();
		}
		
		//反核销
		Model.fhxClick = function(event) {
			var dataMaster = Model.dataMaster;
			var rowID = dataMaster.getCurrentID();
			var writeOffNo = dataMaster.getValue('accountPaymentNo',rowID);
			var enterpriseNo = dataMaster.getValue('enterpriseNo',rowID);
			
			if(writeOffNo==undefined || writeOffNo==null || writeOffNo==''){
				return;
			}
			
			var r=confirm("确定要删除吗？");
			if (r==false){
				return -1;
			}

			//调用核销函数
			var params = {
				"writeOffNo":writeOffNo,
				"enterpriseNo":enterpriseNo
			};

			$.ajax({
	             type: "POST",
	             url: $rootScope.getServiceName() + "/invoice/recSKHX/delete",
	             data: JSON.stringify(params),
	             dataType: "json",
			     contentType: "application/json; charset=utf-8",
	             success: function(data, textStatus){
	 				//console.log('data-->',data);
	 				if(data.code==0){
						systemService.dialogMsg('删除成功！');
						//刷新数据
					    Model.dataMaster.refreshData();
					}else{
						systemService.dialogMsg('删除失败！');
					}
	             },error: function(XMLHttpRequest, textStatus, errorThrown){
	            	 alert('调用失败！',textStatus);
	             }
	         });

			
		};
		
	    return Model;
		
	}	
	
